package com.xea.action;

import java.io.IOException;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.xea.model.Information;
import com.xea.service.InformationService;
import com.xea.service.ServiceFactory;
import com.xea.util.StringUtil;

public class InformationGeneralAction extends DispatcherServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1932405126946046616L;
	
	
	/**
	 * 查询家具常识
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void search(HttpServletRequest request,HttpServletResponse response) throws Exception{
		InformationService informationDao = ServiceFactory.getInformationService();
		String title=request.getParameter("title");
		if(StringUtil.isNullOrEmpty(title))
			title="";
		String pageNo=request.getParameter("pageNo");
		if(StringUtil.isNullOrEmpty(pageNo))
			pageNo="";
		request.setAttribute("pagedata", informationDao.search(title, pageNo));
		response.setCharacterEncoding("utf-8");
		RequestDispatcher dispatcher=request.getRequestDispatcher("inner/general/information/list.jsp");
		dispatcher.forward(request, response);	
	}
	
	/**
	 * 新增家具常识
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	public void add(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
		InformationService informationDao = ServiceFactory.getInformationService();
		response.setCharacterEncoding("GBK"); 
        String title=request.getParameter("title");
        String content=request.getParameter("content");
        informationDao.add(new Information(title, content, new Date(),0));
	}
	
	/**
	 * 查看家具常识详情
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException 
	 */
	public void findById(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
		InformationService informationDao = ServiceFactory.getInformationService();
		String id=request.getParameter("id");
		request.setAttribute("information", informationDao.findById(Integer.valueOf(id)));
		response.setCharacterEncoding("utf-8");
		RequestDispatcher dispatcher=request.getRequestDispatcher(request.getParameter("_url"));
		dispatcher.forward(request, response);
	}
	
	/**
	 * 查看家具常识详情
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException 
	 */
	public void detail(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
		InformationService informationDao = ServiceFactory.getInformationService();
		String id=request.getParameter("id");
		request.setAttribute("information", informationDao.findById(Integer.valueOf(id)));
		response.setCharacterEncoding("utf-8");
		RequestDispatcher dispatcher=request.getRequestDispatcher("inner/general/information/detail.jsp");
		dispatcher.forward(request, response);
	}
	
	/**
	 * 修改家具常识
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public void update(HttpServletRequest request,HttpServletResponse response) throws IOException{
		InformationService informationDao = ServiceFactory.getInformationService();
		String id=request.getParameter("id");
		String title=request.getParameter("title");
        String content=request.getParameter("content");
		response.setCharacterEncoding("utf-8");
		Information information=informationDao.findById(Integer.valueOf(id));
		information.setTitle(title);
		information.setContent(content);
		informationDao.update(information);
	}
	/**
	 * 公开家具常识
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public void pub(HttpServletRequest request,HttpServletResponse response) throws IOException{
		InformationService informationDao = ServiceFactory.getInformationService();
		String id=request.getParameter("id");
		String pub=request.getParameter("pub");
		informationDao.pub(id,Integer.valueOf(pub));
	}
	
}
